//
//  CAExpressListViewController.m
//  LNH
//
//  Created by twentylx on 2018/2/26.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAExpressListViewController.h"
#import "CAExpressListApi.h"

@interface CAExpressListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *expressTableView;
@property (nonatomic, strong) NSArray *expressArray;

@end


static NSString *cellIndentifier = @"expressCell";

@implementation CAExpressListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = RGBCOLOR(241, 241, 241);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
    [self loadexpressData];
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.expressTableView];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    self.navigationItem.title = @"选择快递公司";
}

#pragma mark layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
    
    [_expressTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark get & set
- (UITableView *) expressTableView {
    if (!_expressTableView) {
        _expressTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [_expressTableView setBackgroundColor:RGBCOLOR(241, 241, 241)];
        _expressTableView.dataSource = self;
        _expressTableView.delegate = self;
    }
    return _expressTableView;
}

#pragma mark private method
- (void) loadexpressData {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    CAExpressListApi *expressApi = [[CAExpressListApi alloc] init];
    [expressApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CAExpressListApi *api = (CAExpressListApi *) request;
        if (api.done) {
            self.expressArray = api.responseObject[@"result"];
            [self.expressTableView reloadData];
        } else {
            [SVProgressHUD showErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
    }];
}

#pragma mark - Table view DataSource & Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _expressArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentifier];
    }
    NSDictionary *dict = self.expressArray[indexPath.row];
    
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = dict[@"name"];
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.expressArray[indexPath.row];
    
    if ([self.delegate respondsToSelector:@selector(selectExpressName:)]) {
        [self.delegate selectExpressName:dict[@"name"]];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end


